package com.ueboot.weishop.service.customer.impl;

import com.ueboot.core.utils.DateUtils;
import com.ueboot.core.utils.NumberUtil;
import com.ueboot.weishop.entity.activity.ActivityEntity;
import com.ueboot.weishop.entity.customer.CustomerCouponEntity;
import com.ueboot.weishop.entity.customer.CustomerInfoEntity;
import com.ueboot.weishop.entity.order.OrderIssueEntity;
import com.ueboot.weishop.repository.customer.CustomerCouponRepository;
import com.ueboot.weishop.repository.order.OrderRepository;
import com.ueboot.weishop.service.customer.CustomerCouponService;
import com.ueboot.weishop.service.order.OrderIssueNewService;
import com.ueboot.weishop.service.order.OrderIssueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by Neel on 2017/9/24.
 */
@Slf4j
@Service
public class CustomerCouponServiceImpl implements CustomerCouponService {

    @Autowired
    private CustomerCouponRepository repository;

    @Autowired
    private OrderIssueService orderIssueService;

    @Autowired
    private OrderRepository orderRepository;


    @Override
    public Page<CustomerCouponEntity> findBy(Pageable pageable) {
        return repository.findAll(pageable);
    }

    /**
     * 根据关键字查询
     *
     * @param pageable
     * @param keyword
     * @return
     */
    @Override
    public Page<CustomerCouponEntity> findBy(Pageable pageable, String keyword) {
        CustomerCouponEntity entity = new CustomerCouponEntity();
        ExampleMatcher example = ExampleMatcher.matching()
                .withIgnoreNullValues()
                .withMatcher("name", matcher -> matcher.contains())
                .withMatcher("remark", matcher -> matcher.contains());
        return repository.findAll(Example.of(entity, example), pageable);
    }

    /**
     * 保存代金券
     * @param customer
     * @param activityEntity
     * @param vin
     * @param dealer
     * @param projectCode
     * @param amount
     * @param beginDate
     * @param endDate
     */
    @Override
    public void saveActivityCoupon(CustomerInfoEntity customer, ActivityEntity activityEntity, String customerType, String vin, String dealer, String projectCode, int amount, String beginDate, String endDate) {

        CustomerCouponEntity couponEntity = new CustomerCouponEntity();
        couponEntity.setBeginTime(DateUtils.parseDate(beginDate, DateUtils.FORMAT_DATE_YYYY_MM_DD));
        couponEntity.setEndTime(DateUtils.parseDate(endDate, DateUtils.FORMAT_DATE_YYYY_MM_DD));
        couponEntity.setCustomer(customer);
        couponEntity.setEnabled(Boolean.TRUE);
        couponEntity.setName(activityEntity.getName());
        couponEntity.setVin(vin);
        couponEntity.setSource(activityEntity.getGroupCode());
        couponEntity.setSourceType(customerType);
        couponEntity.setSeqNo(getSeqNo());
        repository.save(couponEntity);

        OrderIssueEntity issueEntity = new OrderIssueEntity();
        issueEntity.setAmount(amount + "");
        issueEntity.setCampaignCode(projectCode);
        issueEntity.setCancel(null);
        issueEntity.setCustomerMobile(customer.getMobile());
        issueEntity.setGoodsName(activityEntity.getName());
        issueEntity.setLimitDeduct(amount + "");
        issueEntity.setName(activityEntity.getName());
        issueEntity.setOpenId(customer.getOpenId());
        issueEntity.setOrderNo(couponEntity.getSeqNo());
        issueEntity.setSeqNo(couponEntity.getSeqNo());
        issueEntity.setTargetDealer(dealer);
        issueEntity.setVin(vin);
        issueEntity.setValidBeginDate(beginDate);
        issueEntity.setValidEndDate(endDate);

        orderIssueService.save(issueEntity);
    }

    @Override
    public String getSeqNo() {
        BigDecimal bigDecimal = orderRepository.getOrderNo();
        String qeqNo = "CO" + NumberUtil.getOrderNo(bigDecimal.longValue());
        return qeqNo;
    }

    @Override
    public List<CustomerCouponEntity> findByVinAndSource(String vin, String groupCode, String customerType) {
        return repository.findByVinAndSourceAndSourceType(vin, groupCode, customerType);
    }

    /**
     * 保存优惠券信息，同时下发DMS表数据
     *
     * @param s
     * @param issueEntity
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 30, propagation = Propagation.REQUIRED)
    public void save(CustomerCouponEntity s, OrderIssueEntity issueEntity) {
        repository.save(s);
        orderIssueService.save(issueEntity);


    }

    @Override
    public CustomerCouponEntity save(CustomerCouponEntity s) {
        return repository.save(s);
    }

    @Override
    public CustomerCouponEntity get(Long id) {
        return repository.findOne(id);
    }

    @Override
    public void delete(Long id) {
        repository.delete(id);
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            repository.delete(id);
        }
    }

	@Override
	public List<CustomerCouponEntity> findByCustomerOpenId(String openId) {
		return repository.findByCustomerOpenId(openId);
	}

}
